﻿using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;
using System.Reflection;

namespace Batman.DLL.UserModule
{
    public class ConfigurationDbContexttFactory : IDesignTimeDbContextFactory<ConfigurationDbContext>
    {
        public ConfigurationDbContext CreateDbContext(string[] args)
        {

            //var env = "Development";
            var env = "Production";
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile($"appsettings.{env}.json")
                .Build();
            var connStr = configuration.GetConnectionString("Default");

            var builder = new DbContextOptionsBuilder<ConfigurationDbContext>();
            builder.UseMySql(connStr, sql =>
            {
                sql.MigrationsAssembly("Batman.DLL.UserModule");
            });

            ConfigurationStoreOptions cso = new ConfigurationStoreOptions();
            cso.ConfigureDbContext = context =>
            {
                context.UseMySql(connStr, sql =>
                {
                    //sql.MigrationsAssembly("IdentityServer");
                    sql.MigrationsAssembly("Batman.DLL.UserModule");
                });
            };

            var production = new ConfigurationDbContext(builder.Options, cso);

            return production;
        }
    }
}
